//
//  ContributeViewController.m
//  HorizonLight
//
//  Created by lanou on 15/9/19.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "ContributeViewController.h"
#import "ContributeScrollView.h"
#import <MessageUI/MessageUI.h>

@interface ContributeViewController ()<UITextFieldDelegate, MFMailComposeViewControllerDelegate,UITextViewDelegate>

@property (nonatomic, strong) ContributeScrollView *contributeScrollView;

@end

@implementation ContributeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.contributeScrollView = [[ContributeScrollView alloc] initWithFrame:kBounds];
    self.contributeScrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight + 100);
    self.contributeScrollView.scrollEnabled = YES;
    self.contributeScrollView.showsVerticalScrollIndicator = NO;//隐藏竖线
    [self.view addSubview:self.contributeScrollView];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightButtonClick:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor blackColor];
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftButtonClick:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    
    //输入框执行代理
    self.contributeScrollView.nickNameTextField.delegate = self;
    self.contributeScrollView.eMailTextField.delegate = self;
    self.contributeScrollView.nameTextView.delegate = self;
    //调用
    [self registerForKeyboardNotifications];
}

#pragma mark------返回按钮
- (void)leftButtonClick:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark------发送按钮
- (void)rightButtonClick:(UIButton *)button
{
    BOOL canSendMail = [MFMailComposeViewController canSendMail];
    if (canSendMail)
    {
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]init];
        [picker setSubject:@"HorizonLight Contribute"];
        [picker setToRecipients:[NSArray arrayWithObject:@"745860245@qq.com"]];
        NSString *emailBody = [NSString stringWithFormat:@"<font color='red'>eMail</font> %@\n%@\n%@", _contributeScrollView.nickNameTextField.text, _contributeScrollView.eMailTextField.text, _contributeScrollView.nameTextView.text];
        [picker setMessageBody:emailBody isHTML:YES];
        [picker setMailComposeDelegate:self];
        
        [self presentViewController:picker animated:YES completion:nil];
    }

}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            [self showMessage:@"邮件发送取消"];
            break;
        case MFMailComposeResultSaved:
            [self showMessage:@"邮件保存成功"];
            break;
        case MFMailComposeResultSent:
            [self showMessage:@"邮件发送成功"];
            break;
        case MFMailComposeResultFailed:
            [self showMessage:@"邮件发送失败"];
            break;
        default:
            break;
    }
    //视图关闭
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)showMessage:(NSString *)message{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

//设置键盘收回
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    [textView resignFirstResponder];
    return YES;
}
//开始输入,隐藏label
- (void)textViewDidChange:(UITextView *)textView
{
    if (0 == textView.text.length)
    {
        self.contributeScrollView.videoNameLabel.alpha = 1;
    }else
    {
        self.contributeScrollView.videoNameLabel.alpha = 0;
    }

}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardDidHideNotification object:nil];
}

//第一响应者
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    switch (textField.tag) {
        case 1000:
        {
            UITextField *textField1 = (UITextField *)[self.view viewWithTag:1001];
            [textField1 becomeFirstResponder];
        }
            break;
            
        case 1001:
        {
            UITextView *textField1 = (UITextView *)[self.view viewWithTag:1002];
            [textField1 becomeFirstResponder];
        }
            break;
            
        default:
            break;
    }
    return YES;
}

#pragma mark ---注册通知的观察者-----
-(void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChanged:) name:UIKeyboardDidShowNotification object:nil];
}

- (void)keyboardFrameChanged:(NSNotification *)notification {
    
    NSDictionary *userInfo = notification.userInfo;
    
    CGRect endFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    UIViewAnimationCurve curve = (UIViewAnimationCurve)[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    CGFloat keyboardY = endFrame.origin.y;
    CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
    UIScrollView *commentView = self.contributeScrollView;

    [UIView animateWithDuration:duration
                          delay:0
                        options:curve << 16
                     animations:^{
                         
                         if (keyboardY == screenHeight)
                         {
                             commentView.contentOffset = CGPointMake(0, -keyboardY + screenHeight) ;
                         }else
                         {
                             commentView.contentOffset = CGPointMake(0, -keyboardY + screenHeight - 230) ;
                         }
                     }
                     completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
